package org.ami.onlineschool.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.ami.onlineschool.dataAccess.StudentListDataAccess;
import org.ami.onlineschool.model.Student;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class UIDefineStudents extends AbstractWorkWindow{

	private JFrame frame;
	private JTextField txtPath;
	private JTextField txtName;
	private JTextField txtEmail;
	private StudentListDataAccess studentListModel;
	private JList lstStudents;

	/**
	 * Launch the application.
	 * @throws UnsupportedLookAndFeelException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws ClassNotFoundException 
	 */
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
		UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

		launchWindow(new File("").getAbsolutePath());
	}

	static void launchWindow(final String dataLocation) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIDefineStudents window = new UIDefineStudents();
					window.setSchoolDataLocation(dataLocation);
					window.initialize();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public UIDefineStudents() {
		//initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				try {
					studentListModel.saveStudentListToDisk();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
			}
		});
		frame.setBounds(100, 100, 450, 300);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		txtPath = new JTextField();
		txtPath.setEnabled(false);
		txtPath.setText(getSchoolDataLocation());
		panel.add(txtPath, BorderLayout.CENTER);
		txtPath.setColumns(10);
		
		JPanel panel_5 = new JPanel();
		panel.add(panel_5, BorderLayout.EAST);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					studentListModel.saveStudentListToDisk();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
			}
		});
		panel_5.add(btnSave);
		
		JPanel panel_1 = new JPanel();
		frame.getContentPane().add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.NORTH);
		panel_2.setLayout(new GridLayout(0, 1, 0, 0));
		
		JPanel panel_3 = new JPanel();
		panel_2.add(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewLabel = new JLabel("Name");
		panel_3.add(lblNewLabel, BorderLayout.WEST);
		
		txtName = new JTextField();
		panel_3.add(txtName, BorderLayout.CENTER);
		txtName.setColumns(10);
		
		JPanel panel_4 = new JPanel();
		panel_2.add(panel_4);
		panel_4.setLayout(new BorderLayout(0, 0));
		
		JLabel lblNewLabel_1 = new JLabel("Email");
		panel_4.add(lblNewLabel_1, BorderLayout.WEST);
		
		txtEmail = new JTextField();
		panel_4.add(txtEmail, BorderLayout.CENTER);
		txtEmail.setColumns(10);
		
		JPanel panel_6 = new JPanel();
		panel_4.add(panel_6, BorderLayout.EAST);
		
		JButton btnNewButton = new JButton("Add");
		panel_6.add(btnNewButton);
		
		JButton btnRemove = new JButton("Remove");
		btnRemove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
			}
		});
		panel_6.add(btnRemove);
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Student st = new Student(txtName.getText(), txtEmail.getText());
				studentListModel.getStudents().add(st);
				UIUtilities.getListModel(lstStudents).addElement(st);
			}
		});
		
		JScrollPane scrollPane = new JScrollPane();
		panel_1.add(scrollPane, BorderLayout.CENTER);
		
		lstStudents = new JList();
		scrollPane.setViewportView(lstStudents);
		
		studentListModel = new StudentListDataAccess(txtPath.getText());
		
		loadStudents();
	}

	private void loadStudents() {
		DefaultListModel listModel = UIUtilities.getListModel(lstStudents);
		UIUtilities.populateModelFromList(listModel, studentListModel.getStudents());
	}
}
